package com.bdqn.t296.noso.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.t296.noso.entity.*;

import com.bdqn.t296.noso.service.RegisterService;
import com.bdqn.t296.noso.service.ReportService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/hospital")
public class hospitalController {
  @Resource
  RegisterService registerService;
    @Resource
    ReportService reportService;
    @RequestMapping("/list")
    public String list(Model model,Register register,@RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex) { //当前显示的页码
        System.out.println("当前页面页码："+pageIndex);
        //患者查询分页
        Page<Register> page = new Page<Register>(pageIndex, 2);
        IPage<Register> userPager = registerService.selectUser(page, register);
        model.addAttribute("user",userPager);
        return "hospital/list";
    }
    @RequestMapping("/selDoor")
    @ResponseBody
    public  Object seldoor(@RequestParam(value = "pageIndex",required = false,defaultValue = "1") Integer pageIndex) { //当前显示的页码
        System.out.println("pageIndex"+pageIndex);
        Page<ReportVo> page = new Page<ReportVo>(pageIndex, 2);
        IPage<ReportVo> reportlist = reportService.selDoor(page);

        return reportlist;
    }
    //页面请求查询科室信息
    @RequestMapping("/selDepartment")
    @ResponseBody
    public Object selDepartment(){
        //new一个实体类
        Departments departments=new Departments();
        //查询所有科室
        //科室查询
        List<Departments>list=registerService.selDepartment(departments);
        //把查到的科室返回前台下拉框中
        return list;

    }

    //页面请求查询医生信息
    @RequestMapping("/selDoctor")
    @ResponseBody
    public Object selDoctor(int departmentId){
        //new一个实体类
        Doctor doctor=new Doctor();
        //把获取过来的科室编号放入实体类中
        System.out.println("科室ID"+departmentId);
        doctor.setDepartmentId(departmentId);
        //根据科室查询旗下所有医生
        List<Doctor> doctors = registerService.selDoctor(doctor);
        for (Doctor a :doctors) {//获得查询记录
            System.out.println();
            System.out.println("床位名字：" +a.getDoctorName());
            System.out.println("床位编号：" + a.getDoctorId());
        }
        //把查到的医生返回前台下拉框中
        return doctors;
    }
    //查询床位
    @RequestMapping("/selBed")
    @ResponseBody
    public Object selBed(int  departmentId){
        Bed bed= new Bed();
        System.out.println("床位ID"+departmentId);
        bed.setDepartmentId(departmentId);
        //查询没有人入住的床位
        bed.setState(0);
        List<Bed> beds = registerService.selBed(bed);
        for (Bed bed1 : beds) {//获得查询记录
            System.out.println();
            System.out.println("床位名字：" + bed1.getBedname());
            System.out.println("床位编号：" + bed1.getBedId());
        }
        return beds;
    }

    //查询折扣
    @RequestMapping("/selDis")
    @ResponseBody
    public Object selDis(){
        List<Moneytype> moneytypes = registerService.selDis();
        for (Moneytype moneytype : moneytypes) {//获得查询记录
            System.out.println();
            System.out.println("社保类型：" + moneytype.getMoneytype());
            System.out.println("社保折扣：" + moneytype.getPercent());
        }
        return moneytypes;
    }
    //添加患者
    @RequestMapping("/addRegister")
    @PostMapping
    public Object addRegister(Register register,HttpServletRequest request){
        System.out.println("进来了增加");
        Register register1=new Register();
        register1.setIdcard(register.getIdcard());
        //把患者的身份证号查询一下看看是否已经办理入住
        List<Register> registers = registerService.selRegister(register1);
        for (Register register2 : registers) {//获得查询记录
            System.out.println();
            System.out.println("已住院人员名字：" + register2.getUserName());
            System.out.println("已住院人员身份证：" +  register2.getIdcard());
        }
        if(registers.size()==0){
            //如果是门诊转过来的用户删除门诊患者信息
            if(register.getReportid()!=0){
                registerService.updZ(register);
            }
            //获取管理人员姓名
            String yonghu = (String)request.getSession().getAttribute("usrName");
            register.setOperator(yonghu);
            //添加患者信息
            int i = registerService.addRegister(register);
            if(i==1){
                //修改床位状态
                int j = registerService.updBed(register);
                if(j==1){
                    return "redirect:/hospital/list";
                }else{
                    return "添加失败";
                }
            }else{
                return "添加失败";
            }
        }else{
            return "该患者已存在，请不要重复添加！";
        }
    }
    //转科室
    @RequestMapping("/updKe")
    @ResponseBody
    public Object updKe(Register register){
        //把用户要住的床改成已住
        registerService.updBed(register);
        //把用户的曾经床位改成空床
//      register.updBstate(register);
        //把用户的信息进行更改
        int i = registerService.updKe(register);
        if(i==1){
            return "变更成功";
        }else{
            return "变更失败";
        }
    }
    //查询患者内容
    @RequestMapping("/registerlist")
    @ResponseBody
    public Object updKe(int registerId) {
        //条件查询
        Register register =new Register();
        register.setRegisterid(registerId);
        List<Register> registerlist = registerService.selRegister(register);
     return registerlist;
    }
}
